package com.hengtuo.fragment;


import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hengtuo.adapter.loginAdapter;
import com.hengtuo.base.BaseFragment;
import com.hengtuo.kaiyuanzhongguo.R;
import com.hengtuo.vo.Entity;

import java.util.ArrayList;
import java.util.List;

/**
 *  我
 * Created by msi on 2016/5/7.
 */
public class Mine_Fragment  extends BaseFragment {
    public static ImageView imageView;
    public static TextView textView2;
    LinearLayout llhead,ll_mine_four;
    LinearLayout lllogin;

    private List<Entity> entityList;
    loginAdapter adapter;
    ListView listView;
    SharedPreferences preferences;
    //查找布局
    @Override
    protected int getLoadViewId() {
        return R.layout.f4;
    }
    //查找控件
    @Override
    protected void initView(View view) {
        imageView = (ImageView)view.findViewById(R.id.imageView);
        textView2 = (TextView)view.findViewById(R.id.textView2);
        llhead = (LinearLayout)view.findViewById(R.id.ll_head);
        lllogin = (LinearLayout)view.findViewById(R.id.ll_login);
        ll_mine_four= (LinearLayout)view.findViewById(R.id.ll_mine_four);
        listView = (ListView)view.findViewById(R.id.ll_login_listview);
    }
    //主方法
    @Override
    protected void initData() {
        preferences=getActivity().getSharedPreferences("aa",getActivity().MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        int num=preferences.getInt("yy",0);
        if(num!=0){
            Toast.makeText(getActivity(), "不是第一次", Toast.LENGTH_SHORT).show();
            ll_mine_four.setVisibility(View.VISIBLE);
            listView = (ListView)view.findViewById(R.id.ll_login_listview);
            initDatt();
            adapter = new loginAdapter(entityList, getActivity());
            listView.setAdapter(adapter);
            llhead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent itent = new Intent(MainActivity.this, LoginYeMian.class);
//                    startActivity(itent);
                }
            });
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    loginAdapter.ViewHolder lv = (loginAdapter.ViewHolder) view.getTag();
                    if (lv.textView3.getText().equals(entityList.get(0).getName())) {
//                        Intent ii = new Intent(MainActivity.this, LoginYeMian.class);
//                        startActivity(ii);
                    } else if (lv.textView3.getText().equals(entityList.get(1).getName())) {
//                        Intent ii = new Intent(MainActivity.this, LoginYeMian.class);
//                        startActivity(ii);
                    } else if (lv.textView3.getText().equals(entityList.get(2).getName())) {
//                        Intent ii = new Intent(MainActivity.this, LoginYeMian.class);
//                        startActivity(ii);
                    } else if (lv.textView3.getText().equals(entityList.get(3).getName())) {
//                        Intent ii = new Intent(MainActivity.this, LoginYeMian.class);
//                        startActivity(ii);
                    }
                }
            });
        }else{
            Toast.makeText(getActivity(), "是第一次", Toast.LENGTH_SHORT).show();
            editor.putInt("yy",222333).commit();
            ll_mine_four.setVisibility(View.GONE);
            listView = (ListView) view.findViewById(R.id.ll_login_listview);
            initDatt();
            adapter = new loginAdapter(entityList, getActivity());
            listView.setAdapter(adapter);
            llhead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent itent = new Intent(MainActivity.this, LoginYeMian.class);
//                    startActivity(itent);
                }
            });
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    loginAdapter.ViewHolder lv = (loginAdapter.ViewHolder) view.getTag();
                    if (lv.textView3.getText().equals(entityList.get(0).getName())) {
//                        Intent ii = new Intent(MainActivity.this, LoginYeMian.class);
//                        startActivity(ii);
                    } else if (lv.textView3.getText().equals(entityList.get(1).getName())) {
//                        Intent ii = new Intent(MainActivity.this, LoginYeMian.class);
//                        startActivity(ii);
                    } else if (lv.textView3.getText().equals(entityList.get(2).getName())) {
//                        Intent ii = new Intent(MainActivity.this, LoginYeMian.class);
//                        startActivity(ii);
                    } else if (lv.textView3.getText().equals(entityList.get(3).getName())) {
//                        Intent ii = new Intent(MainActivity.this, LoginYeMian.class);
//                        startActivity(ii);
                    }
                }
            });
        }
    }

    private void initDatt() {
        String[] ss = new String[]{"消息", "博客", "团队", "反馈"};
        int[] image = new int[]{R.drawable.icon_my_message, R.drawable.icon_my_blog, R.drawable.icon_my_team, R.drawable.icon_my_blog};
        entityList = new ArrayList<>();
        for (int i = 0; i < ss.length; i++) {
            Entity entity = new Entity();
            entity.setName(ss[i]);
            entity.setImage(image[i]);
            entityList.add(entity);
            Log.i("hahahah", entityList.toString());
        }
    }
}
